import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
//
const ControlModal 			= $Lazy.state(()=>import('./cpt/control-modal'))
const ControlModalC 		= $Lazy.state(()=>import('./cpt/control-modal-c'))
const ControlModalAlter		= $Lazy.state(()=>import('./cpt/control-modal-alter'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '主条码号',			name: 'spec_code' },
			/* 业务管理-待确认账单搜索添加按项目名称和自然项目和病人姓名 2021-08-16  --耿琰 */
			{ label: '项目名称',			name: 'kind_id', type:'select' },
			{ label: '自然项目',			name: 'parent_kind_id', type: 'select', loading:true },
			{ label: '病人姓名',			name: 'patient_name' },
			{ label: '医院',			name: 'hosp_id', 				type: 'select' },
			{ label: '区域',			name: 'region_id',	 			type: 'select', },
			{ label: '账期',			name: 'bill_phase_id',	 		type: 'select', },
			{ label: '采集类型',			name: 'collect_type',	 		type: 'select', },
			{ label: '接收标本日期',  	names: ['start_at','end_at'], 	type:'date-range' },
			{ label: '账单创建日期',		names: ['bill_start_at','bill_end_at'], name:'time-null', type:'date-range' },
		],
	}
	
	model = { }
	submit ={}
	query = { status:45305, type:'' }
	
	componentDidMount(){
		this.getSelect()
		
		if(window.isInit){
			this.fetch()
		}
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 医院
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			// 自然项目
			$fn.setRow( forms, 'parent_kind_id', await f.getCache({ api:'nature_project' }))
			// 项目名称
			$fn.setRow( forms, 'kind_id', await f.getCache({ api:'nature_project' }))
			// 区域
			$fn.setRow(forms, 'region_id', await f.getCache({ api:'bs_area' } ) )
			// 账期
			$fn.setRow(forms, 'bill_phase_id', await f.getCache({ api:'billPhaseSelect' } ) )
			// 采集类型
			$fn.setRow(forms, 'collect_type', await f.collect_type)
			this.setState({ forms })
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'bill/salesmanauditlists', { isIndex:true })
	
	// 表单
	cols = [
		{ title: '账单编号',			dataIndex: 'order_sn', 			width:200 },
		{ title: '账期', 			dataIndex: 'phase_str', 		width:100},
		{ title: '条码号', 			dataIndex: 'old_spec_code', 	...$fn.table.spec_code },
		/* 添加字段显示第三方条码 2021-08-16  --耿琰 */
		{ title: '第三方条码', 		dataIndex: 'third_spec_code', 		width:120 },
		{ title: '标本接收时间', 	dataIndex: 'sp_check_time', ...$fn.table.time },
		{ title: '采集类型', 		dataIndex: 'collect_type_name', width:100 },
		{ title: '账单类型', 		dataIndex: 'attribute_name', 	width:100 },
		{ title: '医院名称', 		dataIndex: 'hosp_name', 		width:200 },
		{ title: '自然项目', 		dataIndex: 'parent_kind_name', 	width:250 },
		{ title: '项目名称', 		dataIndex: 'item_name', 		width:250 },
		{ title: '姓名', 			dataIndex: 'patient_name', 		...$fn.table.name },
		{ title: '性别', 			dataIndex: 'sex', 				...$fn.table.sex },
		{ title: '年龄', 			dataIndex: 'age', 				...$fn.table.age },
		{ title: '证件类型', 		dataIndex: 'card_type_name', 	...$fn.table.card_type },
		{ title: '证件号', 			dataIndex: 'card_no', 			...$fn.table.idcard },
		{ title: '业务员', 			dataIndex: 'salesman_user', 	...$fn.table.name },
		{ title: '标准价格', 		dataIndex: 'sprice', 			...$fn.table.num },
		{ title: '折扣率', 			dataIndex: 'percent', 			...$fn.table.num },
		{ title: '实际价格', 		dataIndex: 'price', 			...$fn.table.num },
		{ title: '折扣金额', 		dataIndex: 'perprice', 			...$fn.table.num },
		{ title: '账单生成', 		dataIndex: 'created_at', 		...$fn.table.time },
		{ title: '账单状态', 		dataIndex: 'status_name', 		width:150 },
		{ title: '账单创建日期', 	dataIndex: 'bill_created_at', 		...$fn.table.time },
		{ title: '备注', 			dataIndex: 'remark', 			width:150 },
		TableControls.controls.call(this,{ width:150 })
	]
	// 表格操作按钮
	table_controls = (text, rows) => {
		return [
			TableControls.click.call(this, '操作详情', ()=> this.modal.open(rows) ),
			TableControls.click.call(this, '账单信息', ()=> this.modalc.open(rows) )
		]
	}
	
	// 操作按钮
	controls = () => {
		const { selectedKeys, selectedRows } = this.state
		return [
			// 确认
			Confirm.call(this, '确认', 'bill/salesmannormal',{ msg:`共计【${selectedKeys.length}】条数据，是否确认审核正常？`,query:(keys,rows)=> {
				let uuids = []
				rows.forEach((v, i)=> {
					uuids[i] = v.uuid
				})
				return {uuids,...this.model}
			}} ),
			// 账单异常
			Controls.click.call(this, '账单异常', ()=>this.modalalter.open(selectedRows), {isKeys:true} ),
			// 导出
			Controls.exports.call(this, 'bill/salesmanauditlists')
		]
	}
	
	render(){
		return (
			<>
				<Page title='待确认账单' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { isIndex:true, isInit: window.isInit }) }
				</Page>
				{/* 操作详情 */}
				<ControlModal onRef={ref=>this.modal=ref} />
				{/* 账单信息 */}
				<ControlModalC onRef={ref=>this.modalc=ref} />
				{/* 账单异常 */}
				<ControlModalAlter onRef={ref=>this.modalalter=ref} onRefresh={this.fetch} />
			</>
		)
	}
}

export default Index